<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>1.单例模式</title>
</head>
<body>
	定义：保证一个类仅有一个实例，并提供一个访问他的全局访问点

	<div id="loginBtn">登陆</div>
</body>
<script type="text/javascript">
	var Singleton = function(name){
		this.name = name;
		this.instance = null;
	}

	Singleton.prototype.getName = function() {
		alert(this.name);
	};
	Singleton.getInstance = function(name){
		if(!this.instance){
			this.instance = new Singleton(name);
		}
		return this.instance;
	}

	var a = Singleton.getInstance('sven1');
	var b= Singleton.getInstance('sven2');


	/* 惰性单例 */
	var getSingle = function(fn){
		var result;
		return function(){
			return result || (result = fn.apply(this,arguments));
		}
	}

	var createLoginLayer = function(){
		var div = document.createElement('div');
		div.innerHTML = "我是div";
		div.style.display = "none";
		document.body.appendChild(div)
		return div;
	}

	var createSingleLoginLayer = getSingle(createLoginLayer);

	document.getElementById('loginBtn').onclick = function(){
		var loginLayer = createSingleLoginLayer();
		loginLayer.style.display = 'block';
	}

</script>
</html>